#include <iostream>
#include <bits/stdc++.h>
#include <cstring>
#include <cmath>
using namespace std;
int main(){
	int n,m,sum=0;
	cin>>n>>m;
	int score[m],answer[m],b[m];
	while(n--){
		for(int i=0;i<m;i++){
			cin>>score[i];
			cin>>answer[i];
			cin>>b[m];
		}
		for(int j=0;j<m;j++){
			if(b[j]==answer[j]){
				sum+=score[j];
			}
			
		}
	}
	cout<<sum<<endl;
}
